package com.xxx.ticket;

import javax.xml.stream.FactoryConfigurationError;

/**
 * @version v1.0
 * @Description:
 * @Author zhanzhongxin
 * @Date 2021/7/30 10:40
 * @Copyright 博为峰
 */

/*
    线程安全问题出现的原因:
    1.多个线程同时操作共享数据
    2.操作共享数据的代码有多行

 */
public class Ticket01 extends Thread {

    public static int num = 100;
    public boolean flag = true;

    public void run() {
        while (flag) {//num = 1  0 -1
            if (num > 0) {//t0,t1,t2,t3
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "," + num--);
            } else {
                flag = false;
            }
        }
    }
}
